package com.jt.service;

import com.jt.util.OjbectMapperUtil;
import org.apache.catalina.User;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author Song
 * @since 2020/11/18
 */
@Service
public class UserServiceImpl implements UserService{
    @Override
    public List<User> testhttpclient() {
        List<User> users = new ArrayList<>();
        String url ="http://sso.jt.com/user/testHttpClent";
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet get = new HttpGet(url);
        try {
            HttpResponse response = httpClient.execute(get);
            if (response.getStatusLine().getStatusCode()==200){
                HttpEntity entity = response.getEntity();
                String entitys = EntityUtils.toString(entity, "UTF-8");
                users=OjbectMapperUtil.toOjbect(entitys, users.getClass());
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return users;
    }
}
